#!/bin/bash

# YOLO5 预处理程序构建脚本

set -e

echo "=== YOLO5 预处理程序构建脚本 (aarch64 cross-compilation) ==="

# Check if we're in the right directory
if [ ! -f "CMakeLists.txt" ]; then
    echo "Error: CMakeLists.txt not found. Please run this script from the project root."
    exit 1
fi

# Clean previous build directory
echo "Cleaning previous build directory..."
rm -rf ./build

# Create build directory
echo "Creating build directory..."
mkdir -p build
cd build

# Configure with CMake for aarch64 cross-compilation
echo "Configuring with CMake for aarch64 cross-compilation..."
cmake .. -DCMAKE_BUILD_TYPE=Release \
         -DCMAKE_SYSTEM_NAME=Linux \
         -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
         -DCMAKE_C_COMPILER=/usr/bin/aarch64-linux-gnu-gcc \
         -DCMAKE_CXX_COMPILER=/usr/bin/aarch64-linux-gnu-g++ \
         -DCMAKE_SYSROOT=$HOME/sysroot \
         -DCMAKE_PREFIX_PATH="$HOME/sysroot/usr/lib/aarch64-linux-gnu/cmake;$HOME/sysroot/usr/lib/cmake" \
         -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
         -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
         -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
         -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY

# Build
echo "Building project..."
make -j$(nproc)

echo "=== Build completed successfully! ==="
echo "Executable: build/rga2-pre (aarch64)"
echo ""
echo "Usage examples:"
echo "  ./rga2-pre -h                                    # Show help"
echo "  ./rga2-pre -s /path/to/source -d /path/to/dest   # Process dataset"
echo "  ./rga2-pre -s /path/to/source -d /path/to/dest -r 90  # Rotate 90 degrees"
